﻿namespace Codeblack.TfsScrumboard.UI.Controls
{
    using System;
    using System.Globalization;
    using System.Web;
    using System.Web.UI.WebControls;
    using Codeblack.TfsScrumboard.Resources;
    using Microsoft.TeamFoundation.WebAccess;
    using Microsoft.TeamFoundation.WebAccess.Common;
    using Microsoft.TeamFoundation.WebAccess.Controls;
    using Microsoft.TeamFoundation.WebAccess.UI;

    public class SearchControl : WebAccessUserControl
    {
        #region Control fields

        protected Image impHr;
        protected Literal ltlSearch;
        protected Literal ltlSearchButton;

        #endregion

        #region Page overrides

        protected override void PreRenderUserControl(EventArgs e)
        {
            base.PreRenderUserControl(e);

            // register the script needed by the backlogs
            ScriptHelper.RegisterLocalScriptIncludeCommon(this, "~/Resources/Scripts/ScrumboardSearch.js");
            ScriptHelper.RegisterObjectScript(this, "scrumboardSearch", new object[] { this.UniqueID, base.Locator });
        }

        protected override void RenderUserControl()
        {
            base.RenderUserControl();

            if (!this.Page.IsCallback)
            {
                RenderSearch();
            }
        }

        #endregion

        #region Private methods

        private void RenderSearch()
        {
            this.impHr.ImageUrl = TSWAConfiguration.Current.GetThemePath(TSWAContext.Current.Preferences.Theme) + "Images/hr.gif";
            this.impHr.Width = System.Web.UI.WebControls.Unit.Pixel(0xc2);
            this.impHr.Height = System.Web.UI.WebControls.Unit.Pixel(0x13);
            this.impHr.Visible = true;

            string focusInput = string.Format(CultureInfo.InstalledUICulture, "{0}.SearchHelper.focusInput(\"{1}_search\")", new object[] { this.ClientObjectId, this.ClientID });
            string blurInput = string.Format(CultureInfo.InstalledUICulture, "{0}.SearchHelper.blurInput(\"{1}_search\");", new object[] { this.ClientObjectId, this.ClientID });
            string trapEnterKey = string.Format(CultureInfo.InstalledUICulture, "return {0}.SearchHelper.trapEnterKey(\"{1}_search\", \"search\", event);", new object[] { this.ClientObjectId, this.ClientID });
            this.ltlSearch.Text = string.Format(CultureInfo.InstalledUICulture,
                                                "<input type=\"text\" style=\"width:100%\" class=\"tswa-search\" id=\"{0}_search\" value=\"{1}\" onfocus=\"{2}\" onblur=\"{3}\" onkeypress=\"{4}\" />",
                                                new object[]
                                                {
                                                    this.ClientID,
                                                    HttpUtility.HtmlAttributeEncode(ScrumboardResources.SearchText),
                                                    HttpUtility.HtmlAttributeEncode(focusInput),
                                                    HttpUtility.HtmlAttributeEncode(blurInput),
                                                    HttpUtility.HtmlAttributeEncode(trapEnterKey)
                                                });

            string search = string.Format(CultureInfo.InstalledUICulture, "{0}.SearchHelper.search(\"{1}_search\")", new object[] { this.ClientObjectId, this.ClientID });
            this.ltlSearchButton.Text = string.Format(CultureInfo.InvariantCulture,
                                                      "<input type=\"button\" class=\"tswa-button\" title=\"{0}\" value=\"{1}\" onclick=\"{2}\">",
                                                      new object[]
                                                      {
                                                          HttpUtility.HtmlAttributeEncode(ScrumboardResources.SearchTitle),
                                                          HttpUtility.HtmlAttributeEncode(ScrumboardResources.Search),
                                                          HttpUtility.HtmlAttributeEncode(search)
                                                      });
        }

        #endregion
    }
}
